﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Core.Helper;
using Company.Sqlite.Interfaces;
using Company.Sqlite.Models;
using Company.Sqlite.Repositories;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using 无人值守地磅称重系统.Dialogs;
using 无人值守地磅称重系统.Interfaces;
using 无人值守地磅称重系统.Views;

namespace 无人值守地磅称重系统.ViewModels
{
    public class CarViewModel : ObservableObject
    {
        public Car NewCar { get;set; } = new Car();

        private List<Car> cars;
        public List<Car> Cars { get => cars;set=>SetProperty(ref cars, value); }

        public ICommand InsertCommand { get; }
        public ICommand UpdateCommand { get; }
        public ICommand DeleteCommand { get; }
        public ICommand SearchCommand { get; }
        public ICommand ClearKeywordCommand { get; }


        public ISession Session { get; }

        public ICarRepository CarRepository { get; }

        public CarViewModel(ISession session, ICarRepository carRepository)
        {
            Session = session;
            CarRepository = carRepository;
            Cars = CarRepository.GetAll();

            InsertCommand = new RelayCommand(Insert);
            UpdateCommand = new RelayCommand<Car>(Update);
            DeleteCommand = new RelayCommand<Car>(Delete);
            SearchCommand = new RelayCommand<string>(Search);
            ClearKeywordCommand = new RelayCommand<TextBox>(ClearKeyword);
        }

        private void ClearKeyword(TextBox textBox)
        {
            textBox.Text = string.Empty;
            Cars = CarRepository.GetAll();
        }

        private void Search(string keyword)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                Cars = CarRepository.GetAll();
            }
            else
            {
                Cars = CarRepository.GetAll(keyword);
            }
        }

        private async void Delete(Car car)
        {
            var view = new Dialog("是否执行删除操作？", System.Windows.MessageBoxButton.YesNo);
            bool result = (bool)await DialogHost.Show(view, "RootDialog");
            if (result)
            {
                int count = CarRepository.Delete(car);
                if(count > 0)
                {
                    Session.Show("删除成功");
                    Cars = CarRepository.GetAll();
                }
            }
        }

        private async void Update(Car entity)
        {
            var newEntity = entity.DeepClone();
            var view = new UpdateCarView
            {
                DataContext = new UpdateViewModel<Car>(newEntity)
            };
            bool result  = (bool)await DialogHost.Show(view, "RootDialog");
            if (result)
            {
                entity.Name = newEntity.Name;
                entity.Pweight = newEntity.Pweight;
                entity.Contact= newEntity.Contact;
                entity.Telephone= newEntity.Telephone;
                entity.Rank= newEntity.Rank;
                int count = CarRepository.Update(entity);
                if(count > 0)
                {
                    Session.Show("修改成功");
                }
            }
        }

        private void Insert()
        {
            if (string.IsNullOrEmpty(NewCar.Name))
            {
                Session.Show("请输入物资名");
                return;
            }

            if (CarRepository.Select(NewCar.Name) != null)
            {
                Session.Show("车辆已存在");
                return;
            }

            Car car = new Car();
            car.Name = NewCar.Name;
            car.Pweight = NewCar.Pweight;
            car.Contact = NewCar.Contact;
            car.Telephone = NewCar.Telephone;
            car.Rank = NewCar.Rank;

            int count = CarRepository.Insert(car);
            if (count > 0)
            {
                Session.Show("添加成功");
                Cars = CarRepository.GetAll();
                NewCar.Name = string.Empty;
                NewCar.Pweight = 0d;
                NewCar.Contact = string.Empty;
                NewCar.Telephone = string.Empty;
                NewCar.Rank = 0;
            }
        }
    }
}
